package cn.itcast.dewu.rabbitUtils;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.retry.MessageRecoverer;
import org.springframework.amqp.rabbit.retry.RepublishMessageRecoverer;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FailStrategyConfig {
    //配置一个json的转换器
    @Bean
    public MessageConverter jsonMessageConverter() {
        return new Jackson2JsonMessageConverter();
    }

    /*配置失败的交换机,queue,以及routingkey*/

    @Bean
    public Queue creatErrQueue() {
        return new Queue("errqueue", true); //是否持久化
    }

    @Bean
    public DirectExchange creatErrDirectExchange() {
        return ExchangeBuilder.directExchange("error.direct")
                .durable(true) //是否持久化
                .build();
    }

    @Bean
    public Binding creatErrBinding() {
        return BindingBuilder
                .bind(creatErrQueue())
                .to(creatErrDirectExchange())
                .with("err");
    }
    @Bean
    public MessageRecoverer republishMessageRecoverer(RabbitTemplate rabbitTemplate) {
        return new RepublishMessageRecoverer(rabbitTemplate, "error.direct", "err");
    }
}
